package com.kws.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kws.common.core.enums.ModelOpType;
import com.kws.common.core.sys.Ksys;
import com.kws.common.core.utils.KConstants;
import com.kws.common.core.utils.StringUtils;
import com.kws.common.security.utils.SecurityUtils;
import com.kws.system.api.domain.Rq.CreateDeliverAddressRq;
import com.kws.system.domain.SysDeliveryAddress;
import com.kws.system.domain.info.QueryDeliveryAddressInfo;
import com.kws.system.domain.vo.UpdateDeliveryAddressRq;
import com.kws.system.mapper.SysDeliveryAddressMapper;
import com.kws.system.service.SysDeliveryAddressService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <b>SysDeliveryAddressServiceImpl</b>
 *
 * @description: SysDeliveryAddressServiceImpl <br>
 * @date: 2024/11/6 15:31 <br>
 * @author: lhm <br>
 * @version: 1.0 <br>
 */

@Service
public class SysDeliveryAddressServiceImpl extends ServiceImpl<SysDeliveryAddressMapper, SysDeliveryAddress> implements SysDeliveryAddressService {
    private static final Logger logger = LoggerFactory.getLogger(SysDeliveryAddressServiceImpl.class);

    @Override
    public Page<QueryDeliveryAddressInfo> queryDeliveryAddressPage(Integer pageNum, Integer pageSize, String consigneeName) {
        Page<QueryDeliveryAddressInfo> page = new Page<>(pageNum, pageSize);
        Page<QueryDeliveryAddressInfo> pageInfo = baseMapper.queryDeliveryAddressPage(page,consigneeName);
        return pageInfo;
    }

    @Override
    public boolean createDeliverAddress(CreateDeliverAddressRq rq) {
        SysDeliveryAddress sysDeliveryAddress = new SysDeliveryAddress();
        sysDeliveryAddress.setUserId(rq.getUserId());
        sysDeliveryAddress.setUserName(rq.getUserName());
        sysDeliveryAddress.setOrderNo(rq.getOrderNo());
        sysDeliveryAddress.setConsigneeName(rq.getConsigneeName());
        sysDeliveryAddress.setDeliveryAddress(rq.getDeliveryAddress());
        sysDeliveryAddress.setContactNumber(rq.getContactNumber());
        if (StringUtils.isNotEmpty(rq.getChannel()) && "sys".equals(rq.getChannel())){
            sysDeliveryAddress.setEnable("1");
            sysDeliveryAddress.setCreateUser("sys");
            sysDeliveryAddress.setCreateTime(new Date());
            sysDeliveryAddress.setUpdateUser("sys");
            sysDeliveryAddress.setCreateTime(new Date());
        }else {
            Ksys.setBasicModelData(sysDeliveryAddress, SecurityUtils.getStringUserId(), KConstants.DATA_ENABLE, ModelOpType.CREATE);
        }
        int insert = baseMapper.insert(sysDeliveryAddress);
        if (1 == insert){
            return true;
        }

        return false;
    }

    @Override
    public QueryDeliveryAddressInfo queryDeliveryAddressById(Long id) {
        return baseMapper.queryDeliveryAddressById(id);
    }

    @Override
    public boolean updateDeliveryAddress(UpdateDeliveryAddressRq rq) {
        SysDeliveryAddress sysDeliveryAddress = baseMapper.selectById(rq.getId());
        if (null != sysDeliveryAddress){
            sysDeliveryAddress.setUserId(rq.getUserId());
            sysDeliveryAddress.setUserName(rq.getUserName());
            sysDeliveryAddress.setOrderNo(rq.getOrderNo());
            sysDeliveryAddress.setConsigneeName(rq.getConsigneeName());
            sysDeliveryAddress.setDeliveryAddress(rq.getDeliveryAddress());
            sysDeliveryAddress.setContactNumber(rq.getContactNumber());
            Ksys.setBasicModelData(sysDeliveryAddress,SecurityUtils.getStringUserId(),KConstants.DATA_ENABLE,ModelOpType.UPDATE);
            int i = baseMapper.updateById(sysDeliveryAddress);
            if (1 == i){
                return true;
            }
        }
        return false;
    }

    @Override
    public boolean deleteDeliveryAddressById(Long id) {
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("id",id);
        updateWrapper.set("enable","0");
        updateWrapper.set("update_user",SecurityUtils.getStringUserId());
        updateWrapper.set("update_time",new Date());
        int update = baseMapper.update(null, updateWrapper);
        if (1 == update){
            return true;
        }
        return false;
    }
}
